﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

using BBCommon;

namespace BBBot
{
   public partial class ChatForm : Form
   {
      //private string BBComBotName = "BBBot";
      //private string PenduGameStartLetter = "_";
      //PenduGame PenduGame;

      #region Properties

      private BBBotBusiness BBBotBusiness
      {
         get
         {
            return Program.BBBotBusiness;
         }
      }

      #endregion

      #region Methods

      private List<string> _listAllUser;
      private List<string> ListAllUser
      {
         get
         {
            if (this._listAllUser == null)
               this._listAllUser = new List<string>();

            return this._listAllUser;
         }
         set
         {
            this._listAllUser = value;
         }
      }


      private void Disconnect()
      {
         if (MessageBox.Show("Are you sure you want to disconnect ?", "Confirmation", MessageBoxButtons.YesNo) == DialogResult.Yes)
            Program.Client.Disconnect("Leaving");
      }

      private void CalculTimeElapsed()
      {
         int hour = DateTime.Now.Hour;
         if ((hour > 18) || (hour < 9))
         {
            this.toolStripProgressBar1.ProgressBar.Value = 0;
         }
         else
         {
            this.toolStripProgressBar1.ProgressBar.Value = (hour - 9) * 10;
         }
      }      

      #endregion

      public PlayerCarac Player;

      // Font used
      public Font FontUsrMsg;
      public Font FontTimeStamp;
      public Font FontUsrNick;
      public Font FontAdmMsg;
      public Font FontSysMsg;

      private bool isAlreadyConnecting;
      private bool hasTriedFirstConnection;

      // Is Typing? event
      public sealed class TypingEvent : EventArgs
      {
         public bool isTyping;
         public TypingEvent(bool value)
         {
            isTyping = value;
         }
      }            

      private ConnectForm CoForm;
      private ConnectingForm CuCoForm;      
      private DateTime lastTalkDate;

      public ChatForm()
      {
         InitializeComponent();         

         //this.timer1.Start();
         //this.timer2.Start();
         FontUsrMsg = new Font("Verdana", 8, FontStyle.Regular);
         FontTimeStamp = new Font("Verdana", 8, FontStyle.Regular);
         FontUsrNick = new Font("Verdana", 9, FontStyle.Bold);
         FontAdmMsg = new Font("Verdana", 8, FontStyle.Bold);
         FontSysMsg = new Font("Verdana", 8, FontStyle.Italic);

         //lastTalkerNick = "";
         lastTalkDate = DateTime.MinValue;
         //isTyping = false;

         //lbUserList.SmallImageList = new ImageList();
         //lbUserList.SmallImageList.Images.Add(Resource1.EmptyIcon);
         //lbUserList.SmallImageList.Images.Add(Resource1.Pen.ToBitmap());
         hasTriedFirstConnection = false;
         isAlreadyConnecting = false;

         // Registering on hide/show global event
         Program.HiddenStateChanged += new EventHandler(Program_HiddenStateChanged);
      }
      
      void Program_HiddenStateChanged(object sender, EventArgs e)
      {
         if (Program.IsAppHidden)
         {
            WindowState = FormWindowState.Minimized;
            Hide();
         }
         else
         {
            Show();
            if (WindowState == FormWindowState.Minimized)
               WindowState = FormWindowState.Normal;
         }
      }

      private string GetTimeStamp()
      {
         int elapsed = DateTime.Now.Minute - lastTalkDate.Minute;
         if (elapsed > 0)
         {
            return "[" + DateTime.Now.ToShortTimeString() + "]\t";
         }
         else
         {
            return "\t";
         }
      }

      private void Form1_Shown(object sender, EventArgs e)
      {
         //ballTimer_Tick(null, null);
         Program_HiddenStateChanged(null, null);
         Connect();
      }
     
      public void Connect()
      {
         if (isAlreadyConnecting)
            return;

         isAlreadyConnecting = true;
         // Empty list box
         CuCoForm = new ConnectingForm();
         CoForm = new ConnectForm();

         //this.treeView1.Nodes.Clear();
         //lbUserList.Items.Clear();
         //this.dataGridView_users.Rows.Clear();
         bool connected = false;
         bool firstrun = true;

         while (!connected)
         {
            if (!firstrun || (firstrun && !Program.AppConfig.AutoConnect) || hasTriedFirstConnection && WindowState != FormWindowState.Minimized)
            {
               if (CoForm.ShowDialog(this) != DialogResult.OK)
               {
                  // Clean exit
                  Application.Exit();
                  return;
               }
            }
            firstrun = false;

            // Create the player info
            Player = new PlayerCarac();
            Player.Nick = Program.AppConfig.Nick;
            Player.Type = (PlayerClass)CoForm.lbClass.SelectedIndex;            
            Player.TextColor = Program.AppConfig.TextColor;

            hasTriedFirstConnection = true;
            connected = (CuCoForm.ShowDialog(this) == DialogResult.OK);
         }

         // Change text color
         //tbInput.ForeColor = Program.AppConfig.TextColor;

         isAlreadyConnecting = false;
         this.toolStripStatusLabel3.Text = Player.Nick;
      }

      private void ChatForm_SizeChanged(object sender, EventArgs e)
      {
         Program.IsAppHidden = (WindowState == FormWindowState.Minimized);         
      }

      private void ChatForm_Load(object sender, EventArgs e)
      {
         Program_HiddenStateChanged(null, null);

         this.WindowState = FormWindowState.Minimized;
      }

      private void timer1_Tick(object sender, EventArgs e)
      {
         this.CalculTimeElapsed();
      }

      private void btn_bash_Click(object sender, EventArgs e)
      {
         this.StartBash();
      }

      private void btn_VDM_Click(object sender, EventArgs e)
      {
         this.StartVDM();
      }

      private void btn_pendu_Click(object sender, EventArgs e)
      {
         this.StartPendu();
      }

      #region Methods starting

      private void StartVDM()
      {
         if (this.BBBotBusiness.StartVDM())
         {
            //this.BBBotBusiness.LoadVDM();

            //this.timer_VDM.Interval = int.Parse((this.numericUpDown_VDM.Value * 60000).ToString());
            this.timer_VDM.Interval = 1000;
            this.timer_VDM.Start();
         }
      }

      private void StartBash()
      {
         if (this.BBBotBusiness.StartBash())
         {
            //this.BBBotBusiness.LoadBash();

            //this.timer_bash.Interval = int.Parse((this.numericUpDown_VDM.Value * 60000).ToString());
            this.timer_bash.Interval = 10000;
            this.timer_bash.Start();
         }
      }

      private void StartPendu()
      {
         //this.BBBotBusiness.GetNewPenduWord();
      }

      #endregion

      #region Handlers

      private void timer_bash_Tick(object sender, EventArgs e)
      {
         //this.BBBotBusiness.LoadBash();
      }

      private void timer_VDM_Tick(object sender, EventArgs e)
      {
         //this.BBBotBusiness.LoadVDM();
      }      

      #endregion

   }
}
